package TUILayer;//makes this class a part of the TUILayer package
import ModelLayer.*;
import ControlLayer.*;
import java.util.*;

/**
 * 
 * User interface of the Loan Controller
 * <p>
 * <code>LoanUI</code> class manages information about loans and passes input data to the <code>ControlLayer</code>.
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 */
public class LoanUI
{
    private LoanCtr loanCtr;
    private AddressBook addressMemory;
    private ArrayList<DVD> dvdMemory;
    private DVDCtr dvdCtr;
    private AddressCtr addressCtr;
    private ArrayList<Loan> loanMemoryBackup;
    private boolean existence = false;

    /** 
     * Constructor for objects of <code>LoanUI</code> class gets the backup of the AddressBook and the DVDs and creates a new collection of loans.
     *@param addressMemory stores information about the AddressBook.
     *@param dvdMemory stores an arraylist with all the DVDs.
     */
    public LoanUI(AddressBook addressMemory, ArrayList<DVD> dvdMemory)
    {
        this.addressMemory = addressMemory;
        this.dvdMemory = dvdMemory;
        dvdCtr = new DVDCtr(dvdMemory);
        addressCtr = new AddressCtr(addressMemory);
        loanMemoryBackup = new ArrayList<Loan>();
    }
    /**
     * Starts the User Interface of the Loan system(calls <code>LoanMenu</code> method) and returns a backup of collection of the loans.
     * @param backup keeps an backup of previous ArrayList of loans.
     * @return loanMemoryBackup of the modified collection of loans.
     */
    public ArrayList<Loan> start(ArrayList<Loan> backup)
    {
        loanMemoryBackup = backup;
        loanCtr = new LoanCtr(loanMemoryBackup);
        if(loanMemoryBackup.size() > 0) existence = true;
        loanMenu();
        return loanMemoryBackup;
    }
    
    /**
     * Manages the data flow in the terminal and passes information to the other layers.
     */
    public void loanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            int choice =   writeLoanMenu();
            if (choice == 1)
            {
                if(existence)  findLoan();
            }
            else{if(choice == 2)
                {
                    existence = true;
                    createLoan();
                }
                else{
                    if(choice == 3)
                    {
                        if(existence) updateLoan();
                    }
                    else
                    if(choice == 4)
                    {
                        if(existence) deleteLoan();
                    }
                    else
                    {
                        exit = true;
                        loanMemoryBackup = loanCtr.listAllLoans();
                    }//end else
                }//end else
            }//end else          
        }//end else
    } 
    
    /**
     * Accepts input data and prints information in the Terminal.
     * @return number of type <code>Int<code> as a desired user choice.
     */ 
    public int writeLoanMenu()
    { 
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Show information about a loan");
        System.out.println(" (2) Add Loan");
        System.out.println(" (3) Update loan");
        System.out.println(" (4) Delete loan");
        System.out.println(" (5) Return");
        if(existence == false) System.out.println("NOTE: There are no loans added, please add one first before proceeding");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    /**
     * Creates a new DVD by entering in Terminal:
     *<ul>
     * <li>Friend to whom the dvd was lent.
     * <li>ID of DVD whose copy was lent.
     * <li>Copy's serial number.
     * <li>Information about the loan(<i>unique ID, borrow date, period and state</i>)
     * </ul>
     */
    public void createLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the id of the person to whom the DVD Copy was lent: ");
        long id = keyboard.nextLong();
        while(addressCtr.getPerson(id,false) == null)
        {
            System.out.println("The person ID you input does not exist, please try again: ");
            id = keyboard.nextLong();
        }
        System.out.println("Input the ID of the DVD whose copy you want lent");
        long dvdid = keyboard.nextLong();
        while(dvdCtr.findDVD(dvdid) == null)
        {
            System.out.println("The DVD ID you specified is not valid, please try again: ");
            dvdid = keyboard.nextLong();
        }
        System.out.println("Input the serial number of the copy: ");
        long copyid = keyboard.nextLong();
        while(dvdCtr.findCopy(dvdCtr.findDVD(dvdid),copyid) == null)
        {
            System.out.println("The Copy id you specified is invalid, please try again: ");
            copyid = keyboard.nextLong();
        }
        System.out.println("Please enter the id of the new loan: ");
        long loanid = keyboard.nextLong();        
        while(loanCtr.findLoan(loanid) != null)
        {
            System.out.println("The loan id you specified is already in the system, please try again: ");
            loanid =keyboard.nextLong();
        }
        keyboard.nextLine();
        System.out.println("Input the borrow date of the loan: ");
        String date = keyboard.nextLine();
        System.out.println("Input the period of the loan: ");
        String period = keyboard.nextLine();
        System.out.println("Input the state of the loan: ");
        String state = keyboard.nextLine();
        loanCtr.createLoan(loanid,date,period,state,addressCtr.getPerson(id,false),dvdCtr.findCopy(dvdCtr.findDVD(dvdid),copyid));
    }
    /**
     * Finds a loan by it's ID, entered through the Terminal, and prints out all the information about it. 
     */
    public void findLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the id of the loan whose details you want:");
        long id = keyboard.nextLong();
        while(loanCtr.findLoan(id) == null)
        {
            System.out.println("The id you specified is not registered, please try again:");
            id = keyboard.nextLong();
        }
        Loan tolist = loanCtr.findLoan(id);
        System.out.println("ID: " + tolist.getId());
        System.out.println("Borrow date: " + tolist.getBorrowDate());
        System.out.println("Period: " + tolist.getPeriod());
        System.out.println("State: " + tolist.getState());
        System.out.println("Person id: " + tolist.getPerson().getId());
        System.out.println("Copy serial number: " + tolist.getCopy().getSerialNumber());
        System.out.println();
        System.out.println("Please press ENTER to continue...");
        keyboard.nextLine();
        keyboard.nextLine();
    }
        
    /**
     * Updates all fields(ID, borrow date, period, state) of loan found by ID.
     */
    public void updateLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the id of the loan you want to modify: ");
        long id = keyboard.nextLong();
        while(loanCtr.findLoan(id) == null)
        {
            System.out.println("The id you entered is invalid, please try again:");
            id = keyboard.nextLong();
        }
        System.out.println("Enter the new id: ");
        long newid = keyboard.nextLong();
        while(loanCtr.findLoan(newid) != null)
        {
            System.out.println("The id you input is already in the system, please try again: ");
            newid = keyboard.nextLong();
        }
        keyboard.nextLine();
        System.out.println("Input the new borrow date: ");
        String date = keyboard.nextLine();
        System.out.println("Input the new period: ");
        String period = keyboard.nextLine();
        System.out.println("Input the new state: ");
        String state = keyboard.nextLine();
        loanCtr.updateLoan(id,newid,date,period,state);
    }
    
    /**
     * Deletes a loan by entering its ID in the terminal.
     */
    public void deleteLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the id of the loan you want to delete: ");
        long id = keyboard.nextLong();
        while(loanCtr.findLoan(id) == null)
        {
            System.out.println("The id you entered is invalid, please try again:");
            id = keyboard.nextLong();
        }
        loanCtr.deleteLoan(id);
        if(loanMemoryBackup.size() == 0) existence = false;
    }
}
